package cn.ht.jywrwater.module.ht.controller.admin.meterdata;

import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.meterdata.vo.*;
import cn.ht.jywrwater.module.ht.convert.meterdata.MeterDataConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.meterdata.MeterDataDO;
import cn.ht.jywrwater.module.ht.service.meterdata.MeterDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 水表数据")
@RestController
@RequestMapping("/ht/meter-data")
@Validated
public class MeterDataController {

    @Resource
    private MeterDataService meterDataService;

    @PostMapping("/create")
    @Operation(summary = "创建水表数据")
    @PreAuthorize("@ss.hasPermission('ht:meter-data:create')")
    public CommonResult<Long> createMeterData(@Valid @RequestBody MeterDataCreateReqVO createReqVO) {
        return success(meterDataService.createMeterData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新水表数据")
    @PreAuthorize("@ss.hasPermission('ht:meter-data:update')")
    public CommonResult<Boolean> updateMeterData(@Valid @RequestBody MeterDataUpdateReqVO updateReqVO) {
        meterDataService.updateMeterData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除水表数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:meter-data:delete')")
    public CommonResult<Boolean> deleteMeterData(@RequestParam("id") Long id) {
        meterDataService.deleteMeterData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得水表数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:meter-data:query')")
    public CommonResult<MeterDataRespVO> getMeterData(@RequestParam("id") Long id) {
        MeterDataDO meterData = meterDataService.getMeterData(id);
        return success(MeterDataConvert.INSTANCE.convert(meterData));
    }

    @GetMapping("/list")
    @Operation(summary = "获得水表数据列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:meter-data:query')")
    public CommonResult<List<MeterDataRespVO>> getMeterDataList(@RequestParam("ids") Collection<Long> ids) {
        List<MeterDataDO> list = meterDataService.getMeterDataList(ids);
        return success(MeterDataConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得水表数据分页")
    @PreAuthorize("@ss.hasPermission('ht:meter-data:query')")
    public CommonResult<PageResult<MeterDataRespVO>> getMeterDataPage(@Valid MeterDataPageReqVO pageVO) {
        PageResult<MeterDataDO> pageResult = meterDataService.getMeterDataPage(pageVO);
        return success(MeterDataConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出水表数据 Excel")
    @PreAuthorize("@ss.hasPermission('ht:meter-data:export')")
    @OperateLog(type = EXPORT)
    public void exportMeterDataExcel(@Valid MeterDataExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MeterDataDO> list = meterDataService.getMeterDataList(exportReqVO);
        // 导出 Excel
        List<MeterDataExcelVO> datas = MeterDataConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "水表数据.xls", "数据", MeterDataExcelVO.class, datas);
    }

}
